package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.bean.DealerCarBean;
import com.bjesc_b2c.app.appraiset.bean.XDPostBean;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.http.IApiFragmentRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.XDModel;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.BasicInfoBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/7/27.
 */

public class XDModelImpl implements XDModel {
    private CallBack callBack;

    public XDModelImpl(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void getDealerCar(Context context, XDPostBean bean) {

        HashMap<String, Object> build = new MapUtil()
                .put("page", bean.getPage())
                .put("status", bean.getStatus())
                .put("dealer_id", bean.getDealer_id())
                .put("start_time", bean.getStart_time())
                .put("end_time", bean.getEnd_time())
                .put("type", bean.getType())
                .put("keyword", bean.getKeyword())
                .put("vin", bean.getVin())
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiFragmentRequest.class).getDealerCars(),
                new BaseSubscriber<TokenBean<DealerCarBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<DealerCarBean> o) {
                        if(callBack!=null){
                            callBack.callback(o.getData());
                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }
                    @Override
                    public void onStart() {
                        if(callBack!=null){
                            callBack.loadStart();
                        }
                    }
                    @Override
                    public void onCompleted() {
                        if(callBack!=null){
                            callBack. loadCompleted();
                        }
                    }
                });
    }

    @Override
    public void addCar(Context context,String dealerId, String vin) {
        HashMap<String, Object> build = new MapUtil()
                .put("check_area_id",dealerId)
                .put("vin_number",vin)
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).addCar(),
                new BaseSubscriber<TokenBean<BasicInfoBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }
                    @Override
                    public void onStart() {
                        if(callBack!=null){
                            callBack.loadStart();
                        }
                    }
                    @Override
                    public void onCompleted() {
                        if(callBack!=null){
                            callBack. loadCompleted();
                        }
                    }
                });
    }

    @Override
    public void alreadyInventory(Context context, String dealerId) {
        HashMap<String, Object> build = new MapUtil()
                .put("dealer_id",dealerId)
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).lookInventory(),
                new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {
                        if(callBack!=null){
                            callBack.onSuccess(o);
                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }
                    @Override
                    public void onStart() {
                        if(callBack!=null){
                            callBack.loadStart();
                        }
                    }
                    @Override
                    public void onCompleted() {
                        if(callBack!=null){
                            callBack. loadCompleted();
                        }
                    }
                });
    }
    @Override
    public void carUnusual(Context context, String dealerId,String carId ) {
        HashMap<String, Object> build = new MapUtil()
                .put("id",dealerId)
                .put("car_id",carId)
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).carUnusual(),
                new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }
                    @Override
                    public void onStart() {
                        if(callBack!=null){
                            callBack.loadStart();
                        }
                    }
                    @Override
                    public void onCompleted() {
                        if(callBack!=null){
                            callBack. loadCompleted();
                        }
                    }
                });
    }

    public interface CallBack{
        void callback(DealerCarBean bean);
        void loadStart();
        void loadCompleted();
        void loadFailure();
        void onSuccess(TokenBean bean);
    }
}
